
var htmlparser = require("htmlparser2");

/**
 * 将获取的xml解析为json键值对
 * @param  {string} xml XML字符串
 * @return {object}     解析好的结果对象
 */
function parseXML(xml) {

    var result = [];

    var item = {};
    var parser = new htmlparser.Parser({
        onopentag: function(name, attribs){
            if(name === "slice"){
                console.log(attribs.title);
                item.title = attribs.title;
            }
        },
        ontext: function(text){
            console.log("-->", text);
            item.value = text;
        },
        onclosetag: function(tagname){
            if(tagname === "slice"){
                //console.log("结束一条！");
                result.push(item);
                item = {};
            }
        }
    }, {decodeEntities: true});

    parser.write(xml);
    parser.end();

    console.log(result);
    return result;
}

export default parseXML;